<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use App\Model\UserModel;

class UserEditController extends Controller
{

    public function showUser(Request $request)
    {
      try{
            $userId = $request->input('user_id');
            $userMaster = new UserModel();

            if (empty($userId)) {
                return view('/admin/userEdit', array(
                    'userInfo' => $userMaster
                ));
            } else {
                $data = [
                    $userId
                ];
                $userInfo = $userMaster->getUser($data);
                return view('/admin/userEdit', array(                    'userInfo' => $userInfo[0]
                ));
            }
        }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    public function saveUser(Request $request)
    {
       try{
            $userMaster = new UserModel();
            $userInfo = $request->input('userMaster');
            $userId = $userInfo['user_id'];
            $username = $userInfo['user_name'];

            $flag = $userMaster->getUserByName($username, $userId);

            if ($flag == true) {
                echo ("<script type='text/javascript'> alert('ユーザーが存在している');history.go(-1);</script>");
                exit();
            }

            if (empty($userId)) {
                $userInfo['auth'] = '4';
                $userInfo['corp_id'] = cookie::get('corp_id');
                $userInfo['created_at'] = now();
                $userInfo['updated_at'] = now();
                $userInfo['delete_flg'] = 0;
                $flag = $userMaster->addUser($userInfo);
                if ($flag) {
                    // return view('/admin/user')->with('success', '新規成功！');
                    return redirect('/admin/user')->with('success', '新規成功！');
                }
            } else {

                $list = 'user_id';
                $userInfo['updated_at'] = now();

                $flag = $userMaster->updUser($userId, $list, $userInfo);
                if ($flag) {
                    return redirect('/admin/user')->with('success', '修正成功！');
                }
            }
        }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    public function checkUserMaster(Request $request)
    {
       try{
            $userMaster = new UserModel();
            // $userInfo = $request->input('userMaster');
            // $userId = $userInfo['user_name'];
            $username = 'pesttec';

            $flag = $userMaster->getUserName($username);

            if ($flag == true) {
                echo ("<script type='text/javascript'> alert('此用户名已存在');history.go(-1);</script>");
            } else {
                echo ("<script type='text/javascript'> alert('可以注册');history.go(-1);</script>");
            }
        }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }
}

